//Treats for the Cows G/S:https://www.luogu.com.cn/problem/P2858

#include <iostream>

using namespace std;

const int N = 2e3 + 10;

int a[N];
int f[N][N];
int n;
int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)cin >> a[i];
	
	for(int len = 1; len <= n; len++)
	{
		for(int i = 1; i + len-1 <= n; i++)
		{
			int j = i +len -1;
			int k = n - len + 1;
			f[i][j] = max(f[i][j-1] + k *a[j],f[i+1][j] + k * a[i]);
		}
	}
	cout << f[1][n];
	return 0;
}
